﻿#include "componentlistwidget.h"
#include "unitlistwidgetitem.h"
#include "globaldata.h"
#include <QFileInfo>
#include <QMessageBox>
#include <QDrag>
#include <QMimeData>
#include <QBuffer>
#include <QCoreApplication>

ComponentListWidget::ComponentListWidget(QWidget* parent)
    :QListWidget(parent)
{
    initUI();
    setupGroups();
    setDragEnabled(true);
    setDragDropMode(QAbstractItemView::DragOnly);
    connect(this, &QListWidget::itemClicked, this, &ComponentListWidget::onItemClicked);
}

void ComponentListWidget::startDrag(Qt::DropActions supportedActions)
{
    UnitListWidgetItem* item = dynamic_cast<UnitListWidgetItem*>(currentItem());
    if (item)
    {
        QPixmap pixmap = GlobalData::getInstance().getCurPixmap();

        // 将图片序列化为PNG格式
        QByteArray ba;
        QBuffer buffer(&ba);
        buffer.open(QIODevice::WriteOnly);
        pixmap.save(&buffer, "PNG");

        QMimeData* mimeData = new QMimeData;
        mimeData->setData("image/png", ba);

        QDrag* drag = new QDrag(this);
        drag->setMimeData(mimeData);
        drag->setPixmap(pixmap.scaled(100, 100, Qt::KeepAspectRatio));
        drag->exec(Qt::CopyAction);
    }
}

void ComponentListWidget::onItemClicked(QListWidgetItem *item)
{
    if (!item->data(Qt::UserRole).toBool())
        return;
    QStyle* style = this->style();
    QIcon collapsedIcon = style->standardIcon(QStyle::SP_ArrowRight);
    QIcon expandedIcon = style->standardIcon(QStyle::SP_ArrowDown);

    for (Group& group : m_groups)
    {
        if (group.header != item)
            continue;
        group.isExpanded = !group.isExpanded;
        item->setIcon(group.isExpanded ? expandedIcon : collapsedIcon);

        for (QListWidgetItem* child : group.children)
            setRowHidden(row(child), !group.isExpanded);
        break;
    }
}

void ComponentListWidget::initUI()
{
    setIconSize(QSize(30,30));
}

void ComponentListWidget::setupGroups()
{
    QStyle* style = this->style();
    QIcon collapsedIcon = style->standardIcon(QStyle::SP_ArrowRight);
    QFont headerFont;
    headerFont.setBold(true);

    QString catalogPath = QString(QCoreApplication::applicationDirPath() + "/catalog.xml");
    if (!QFileInfo::exists(catalogPath))
    {
        QMessageBox::warning(
            nullptr,
            "文件未找到",
            "文件不存在:\n" + catalogPath,
            QMessageBox::Ok
        );
    }

    GlobalData::getInstance().initCatalog(catalogPath);
    for(const Units& units : GlobalData::getInstance().getCatalog())
    {
        Group group;
        group.header = new QListWidgetItem(collapsedIcon, units.name);
        group.header->setData(Qt::UserRole, true);
        group.header->setFont(headerFont);
        addItem(group.header);

        for(const Unit& unit : units.units)
        {
            QIcon icon;
            icon.addFile(unit.iconPath, QSize(200, 200));
            UnitListWidgetItem* item = new UnitListWidgetItem(icon,unit.name);
            item->setUnit(unit);
            addItem(item);
            group.children.append(item);
            setRowHidden(row(item), true);
        }
        group.isExpanded = false;
        m_groups.append(group);
    }
}
